//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    //头部菜单
    topList: [
      "店铺首页", "全部商品", "新品上架"
    ],
    topIndex: 0,
    index:1,
    rocomcon:[],
    newcon:[],
    product:[],
    product2:[],
    islogin:true
  },
  switchTopTab: function (e) {
    var topIndex = parseInt(e.currentTarget.dataset.index);
    console.log("当前点击--->", this.data.topList[topIndex])
    this.setData({
      topIndex: topIndex
    })
  },
  bindinfo: function (e) {
    wx.navigateTo({
      url: '../info/info?product_id=' + e.currentTarget.dataset.productid
    })

  },
  onGotUserInfo: function (res) {
    var that = this
    console.log(res.detail.errMsg)
    if (res.detail.errMsg == "getUserInfo:ok") {
      app.getsession(function(){
        this.setData({
          islogin: true
        })
        wx.showTabBar()
      }.bind(this))
    }
  },
  sort:function(e){
    
    var index = e.currentTarget.dataset.index
    if (index==2)
    return;
    this.setData({
      index: index
    })
    var prodada = this.data.product
    for (let item of prodada) {
      item.prices_sale = item.prices.sale
      item.qty_sold = item.qty.sold
    }
    switch(index){
      case '1':
        this.setData({
          product: this.data.product2,
        })
        break;
      case '2':
        break;
      case '3':

        test(prodada, { "qty_sold": "asc" })
        this.setData({
          product: prodada
        })
        break;
      case '4':
        test(prodada, { "prices_sale": "asc" })
        this.setData({
          product: prodada
        })
        break;
    }
    function SortByProps(item1, item2) {
      "use strict";
      var props = [];
      for (var _i = 2; _i < arguments.length; _i++) {
        props[_i - 2] = arguments[_i];
      }
      var cps = [];
      var asc = true;
      if (props.length < 1) {
        for (var p in item1) {
          if (item1[p] > item2[p]) {
            cps.push(1);
            break;
          } else if (item1[p] === item2[p]) {
            cps.push(0);
          } else {
            cps.push(-1);
            break;
          }
        }
      } else {
        for (var i = 0; i < props.length; i++) {
          var prop = props[i];
          for (var o in prop) {
            asc = prop[o] === "asc";
            if (item1[o] > item2[o]) {
              cps.push(asc ? 1 : -1);
              break;
            } else if (item1[o] === item2[o]) {
              cps.push(0);
            } else {
              cps.push(asc ? -1 : 1);
              break;
            }
          }
        }
      }

      for (var j = 0; j < cps.length; j++) {
        if (cps[j] === 1 || cps[j] === -1) {
          return cps[j];
        }
      }
      return 0;
    };
    function test(items, propOrders) {
      items.sort(function (a, b) {
        return SortByProps(a, b, propOrders);
      });
    }
  },
  addshopcar: function (e) {
    var p = this.data.product[e.currentTarget.dataset.index]
    var proarray = []
    var proobj = {
      productId: p.id,
      price: p.prices.sale,
      img: p.imgs[0].href,
      title: p.title,
      specs: p.specs
    }
    var repro = wx.getStorageSync('shop_pro')
    if (wx.getStorageSync('shop_pro') == '') {
      repro = []
    }
    var havesame = false;
    for (let item of repro) {
      if (proobj.productId == item.proobj.productId) {
        item.pronum += 1
        havesame = true
        console.log('重复商品')
      }
    }
    if (havesame == false) {
      var pronum = 1
      var shop_pro = {
        proobj: proobj,
        pronum: pronum,
        status: false
      }
      repro.push(shop_pro)
    }
    console.log(repro)
    console.log('settarbar')

    wx.setStorage({
      key: 'shop_pro',
      data: repro
    })
    wx.showToast({
      title: '添加成功',
      duration: 1000
    })
    app.gettotnum();
  },
  onLoad: function () {
    app.req("get","/api/reclist?reclistId=home", {}, function (res) {
  this.setData({
    rocomcon: res.data.data.content
  })
    }.bind(this))
    app.req("get", "/api/reclist?reclistId=home", {}, function (res) {
      this.setData({
       newcon: res.data.data.content
      })
    }.bind(this))
    app.req("get", "/api/products", {}, function (res) {
      this.setData({
        product: res.data.data.content,
        product2: res.data.data.content
      })
    }.bind(this))
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },
  onShareAppMessage: function () {
    return {
      title: '十里芳菲',
      path: 'pages/route2/index/index'
    }
  },
  onShow(){
    app.gettotnum();
    app.userLogin()
    app.islogin(function (res) {
      if (res.data==''){
        this.setData({
          islogin: false
        })
        wx.hideTabBar()
      }
    }.bind(this),function(res){
        this.setData({
          islogin:false
        })
        wx.hideTabBar()
    }.bind(this))
  },
  
})


